package com.dream.wuliu.utils;

import com.dream.vo.api.ApiResult;
import com.dream.wuliu.base.AppConstants;
import com.dream.wuliu.view.BaseActivity;
import com.dream.wuliu.view.R;

public abstract class ServiceHelper<T> {
	
	public String execute(Class<T> cls,BaseActivity activity){
		
		
		try{
    		T service = activity.getServiceBuilder().buildService(cls);
			ApiResult result = doService(service);
			if(result.isResult()){
				 String message = result.getMessage();
				 //更新成功的情况
				 if(MyUtils.isBlank(message)){
					 message = AppConstants.STR_TRUE;
				 }
				 if(MyUtils.isNotBlank(getSuccessMsg())){
					 activity.toastMsg(getSuccessMsg());
				 }
				 //查询的情况
				 return message;
			}
			
			if(MyUtils.isNotBlank(result.getMessage())){
				activity.toastMsg(result.getMessage());
			}
			activity.toastMsg(getFailMsg(activity));
		}catch(Exception ex){
			activity.handleException(ex);
		}
		
		return AppConstants.STR_EMPTY;
	}
	
	public abstract ApiResult doService(T service);
	
	public String getSuccessMsg(){return AppConstants.STR_EMPTY;}
	public String getFailMsg(BaseActivity activity){ return activity.getStr(R.string.err_req);}
}
